package com.hrs.core.controller;

import com.hrs.common.anno.NoNeedLogin;
import com.hrs.common.anno.NoValidPrivilege;
import com.hrs.common.constant.SwaggerConst;
import com.hrs.common.enums.*;
import com.hrs.common.result.Result;
import com.hrs.core.service.FileService;
import com.hrs.core.service.RoleService;
import com.hrs.enity.db.File;
import com.hrs.enity.db.Role;
import com.hrs.enity.vo.EnumVo;
import com.hrs.enity.vo.FileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author 摆渡人
 * @description
 * @date 2022/12/6 13:03
 */
@Slf4j
@Api(tags = {SwaggerConst.Admin.MANAGER_SERVICE})
@RestController
public class CommonController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private FileService fileService;

    @ApiOperation(value = "根据类型获取对应的枚举列表",notes = "@author 摆渡人")
    @GetMapping("/common/enums/{type}")
    @NoNeedLogin
    public Result<List<EnumVo>> getEnumsByType(@PathVariable("type") Integer type) {
        switch (type) {
            case 0:
                //国家
                return Result.success(Nationality.getEnumVos());
            case 1:
                //民族
                return Result.success(Nation.getEnumVos());
            case 2:
                //政治面貌
                return Result.success(Politics.getEnumVos());
            case 3:
                //学历
                return Result.success(EduBackground.getEnumVos());
            case 4:
                //信仰
                return Result.success(Faith.getEnumVos());
            case 5:
                //角色
                return Result.success(com.hrs.common.enums.Role.getEnumVos());
            case 6:
                //性别
                return Result.success(Sex.getEnumVos());
            default:
                return Result.error("未知选项");
        }
    }

    @ApiOperation(value = "角色下拉框")
    @GetMapping("/common/role")
    @NoNeedLogin
    public Result<List<Role>> listRole(){
        return Result.success(roleService.lambdaQuery().list());
    }

    @ApiOperation(value = "图片上传")
    @PostMapping("/common/file/upload")
    @NoValidPrivilege
    public Result<File> localUpload(MultipartFile file) {
        return fileService.localUpload(file);
    }

    @ApiOperation(value = "图片下载")
    @GetMapping("/common/file/download")
    @NoValidPrivilege
    public void localDownload(String path, HttpServletResponse response) {
        fileService.localDownload(response,path);

    }

}
